package com.bh.web.controller.backGoods;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bh.common.annotation.Log;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.common.enums.BusinessType;
import com.bh.backGoods.domain.JcBackGoods;
import com.bh.backGoods.service.IJcBackGoodsService;
import com.bh.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import com.bh.common.core.page.TableDataInfo;

/**
 * 内部返货Controller
 *
 * @author slx
 * @date 2024-04-03
 */
@Api(tags = {"内部返货API"})
@RestController
@RequestMapping("/backGoods/backGoods")
public class JcBackGoodsController extends BaseController {
    @Autowired
    private IJcBackGoodsService jcBackGoodsService;

    /**
     * 查询内部返货列表
     */
    @ApiOperation("查询内部返货列表")
    @PreAuthorize("@ss.hasPermi('backGoods:backGoods:list')")
    @GetMapping("/list")
    public TableDataInfo list(JcBackGoods jcBackGoods) {
        jcBackGoods.setOrgIds(getOrgIds());
        startPage();
        List<JcBackGoods> list = jcBackGoodsService.selectJcBackGoodsList(jcBackGoods);
        return getDataTable(list);
    }

    /**
     * 导出内部返货列表
     */
    @PreAuthorize("@ss.hasPermi('backGoods:backGoods:export')")
    @Log(title = "内部返货", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JcBackGoods jcBackGoods) {
        List<JcBackGoods> list = jcBackGoodsService.selectJcBackGoodsList(jcBackGoods);
        ExcelUtil<JcBackGoods> util = new ExcelUtil<JcBackGoods>(JcBackGoods.class);
        util.exportExcel(response, list, "内部返货数据");
    }

    /**
     * 获取内部返货详细信息
     */
    @ApiOperation("获取内部返货详细信息")
    @PreAuthorize("@ss.hasPermi('backGoods:backGoods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(jcBackGoodsService.selectJcBackGoodsById(id));
    }

    /**
     * 新增内部返货
     */
    @ApiOperation("新增内部返货")
    @PreAuthorize("@ss.hasPermi('backGoods:backGoods:add')")
    @Log(title = "内部返货", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JcBackGoods jcBackGoods) {
        return toAjax(jcBackGoodsService.insertJcBackGoods(jcBackGoods));
    }

    /**
     * 修改内部返货
     */
    @ApiOperation("修改内部返货")
    @PreAuthorize("@ss.hasPermi('backGoods:backGoods:edit')")
    @Log(title = "内部返货", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JcBackGoods jcBackGoods) {
        JcBackGoods old = jcBackGoodsService.selectJcBackGoodsById(jcBackGoods.getId());
        // 只有状态为创建才能修改
        if (!"0".equals(old.getStatus())) {
            return error("只有状态为创建才能修改");
        }
        return toAjax(jcBackGoodsService.updateJcBackGoods(jcBackGoods));
    }

    /**
     * 修改内部返货
     */
    @ApiOperation("修改内部返货")
    @PreAuthorize("@ss.hasPermi('backGoods:backGoods:send')")
    @Log(title = "内部返货", businessType = BusinessType.UPDATE)
    @PutMapping("/editStatus")
    public AjaxResult editStatus(@RequestBody JcBackGoods jcBackGoods) {
        return toAjax(jcBackGoodsService.updateJcBackGoodsStatus(jcBackGoods));
    }

    /**
     * 删除内部返货
     */
    @ApiOperation("删除内部返货")
    @PreAuthorize("@ss.hasPermi('backGoods:backGoods:remove')")
    @Log(title = "内部返货", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        // 只有状态为创建才能删除
        List<JcBackGoods> jcBackGoodsList = jcBackGoodsService.listByIds(Arrays.asList(ids));
        if (jcBackGoodsList.stream().anyMatch(jcBackGoods -> !"created".equals(jcBackGoods.getStatus()))) {
            return AjaxResult.warn("只能删除状态为创建的记录");
        }
        return toAjax(jcBackGoodsService.deleteJcBackGoodsByIds(ids));
    }
}
